package com.socialcoco.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.socialcoco.entity.Chat;
import com.socialcoco.entity.Comment;
import com.socialcoco.exception.myExection;
import com.socialcoco.respone.BaseResponse;
import com.socialcoco.respone.ResultUtils;
import com.socialcoco.respone.resultenum;
import com.socialcoco.serviceImpl.ChatImageServiceImpl;
import com.socialcoco.serviceImpl.ChatServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 赵志浩
 * @since 2022-03-17 09:16:50
 */
@RestController
@RequestMapping("/socialcoco/user")
public class ChatController {

    @Autowired
    private ChatServiceImpl chatService;
    @GetMapping("/chatHistory/{uid1}/{uid2}")
    @ApiOperation("获取聊天记录")
    public BaseResponse<Object> chatHistory(@PathVariable Integer uid1,@PathVariable Integer uid2,@RequestHeader("token") String token){
        if(uid1==null||uid1<0||uid2<0){
            throw new myExection(resultenum.BODY_NULL.getCode(),resultenum.BODY_NULL.getMsg());
        }
        QueryWrapper<Chat> commentQueryWrapper=new QueryWrapper<>();
        commentQueryWrapper.eq("uid1",uid1).eq("uid2",uid2).or().eq("uid1",uid2).eq("uid2",uid1).orderByAsc("id");
        List<Chat> list = chatService.list(commentQueryWrapper);
        return ResultUtils.success(list);
    }
    @PostMapping("/addChat")
    @ApiOperation("添加聊天记录")
    public BaseResponse<Object> addChat(@RequestBody Chat chat,@RequestHeader("token") String token){
        if(chat==null){
            throw new myExection(resultenum.BODY_NULL.getCode(),resultenum.BODY_NULL.getMsg());
        }
        chat.setCreateTime(new Date());
        chat.setActive(0);
        chatService.save(chat);
        QueryWrapper<Chat> commentQueryWrapper=new QueryWrapper<>();
        commentQueryWrapper.eq("create_time", chat.getCreateTime());
        List<Chat> list = chatService.list(commentQueryWrapper);
        return ResultUtils.success(list);
    }

}

